<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BoxAddress extends Model
{
    public $table = 'box_address';

     public static function boxUpdate($box_id,$data = []){
         if (!array_key_exists('wechat_id',$data)) {
             return ['code' => false,'msg' => '缺少用户ID'];
         }
         if (array_key_exists('user_address_id',$data)) {
             $param = [
                 'wechat_id'=>$data['wechat_id'],
                 'box_id'=>$box_id,
                 'user_address_id'=>$data['user_address_id'],
                 'type'=>$data['type'],
             ];
             $row = BoxAddress::boxCopyAddress($param);
         }else{
             $typeArr = ['box','care'];
             if (!in_array($data['type'],$typeArr)) {
                 return ['code' => false,'msg' => '类型错误'];
             }
             $row = BoxAddress::where(['box_id'=>$box_id,'type'=>$data['type']])->where('wechat_id',$data['wechat_id'])->first();
             if (empty($row)) {
                 return ['code' => false,'msg' => '地址不存在'];
             }
             if (array_key_exists('phone',$data)) {
                 $row->phone = $data['phone'];
             }
             if (array_key_exists('username',$data)) {
                 $row->username = $data['username'];
             }
             if (array_key_exists('address',$data)) {
                 $row->address = $data['address'];
             }
             if (array_key_exists('province',$data)) {
                 $row->province = $data['province'];
             }
             if (array_key_exists('city',$data)) {
                 $row->city = $data['city'];
             }
             if (array_key_exists('area',$data)) {
                 $row->area = $data['area'];
             }
             $row->Save();
         }
         return ['code' => true,'msg' => 'ok','data'=>$row];
     }

     public static function boxCopyAddress($data = []){
        switch ($data['type']) {
             case 'box':
                 $row = Box::where(['wechat_id'=>$data['wechat_id']])->find($data['box_id']);
                 break;
             case 'care':
                 $row = BoxCare::where(['wechat_id'=>$data['wechat_id']])->find($data['box_id']);
                 break;
             default:
                 return ['code' => false,'msg' => '类型错误'];
                 break;
        }
        if (empty($row)) {
             return ['code' => false,'msg' => '盒子不存在'];
        }
        $UserAddress = UserAddress::where('wechat_id',$data['wechat_id'])->find($data['user_address_id']);
        if (empty($UserAddress)) {
             return ['code' => false,'msg' => '用户地址不存在'];
        }
        $BoxAddress = BoxAddress::where(['box_id'=>$data['box_id'],'type'=>$data['type']])->where('wechat_id',$data['wechat_id'])->first();
        if (empty($BoxAddress)) {
             $BoxAddress = new BoxAddress;
             $BoxAddress->box_id = $data['box_id'];
             $BoxAddress->wechat_id = $data['wechat_id'];
             $BoxAddress->type = $data['type'];
             $BoxAddress->user_address_id = $UserAddress->id;
        }
        $BoxAddress->username = $UserAddress->username;
        $BoxAddress->province  = $UserAddress->province;
        $BoxAddress->city = $UserAddress->city;
        $BoxAddress->area = $UserAddress->area;
        $BoxAddress->address = $UserAddress->address;
        $BoxAddress->phone = $UserAddress->phone;
        $BoxAddress->Save();
        return ['code' => 'SUCCESS','msg' => 'ok','data'=>$BoxAddress];
    }


}
